const sass = require('sass')
const path = require('path')
const loadGruntTasks = require('load-grunt-tasks')
const data = {
    menus: [{
        name: 'Home',
        icon: 'aperture',
        link: 'index.html'
    },
    {
        name: 'Features',
        link: 'features.html'
    },
    {
        name: 'About',
        link: 'about.html'
    },
    {
        name: 'Contact',
        link: '#',
        children: [{
            name: 'Twitter',
            link: 'https://twitter.com/w_zce'
        },
        {
            name: 'About',
            link: 'https://weibo.com/zceme'
        },
        {
            name: 'divider'
        },
        {
            name: 'About',
            link: 'https://github.com/zce'
        }
        ]
    }
    ],
    pkg: require('./package.json'),
    date: new Date()
}

module.exports = grunt => {
    grunt.initConfig({
        clean: {
            src: ['temp/**', 'dist/**']  // 可以使用通配符 *表示当前目录下所有以.js结尾的文件，**表示所有子目录和子目录下的文件
        },
        // 编译sass文件任务
        sass: {
            options: {
                sourceMap: true,
                implementation: sass // 通过sass模块来实现sass文件的编译
            },
            main: {
                expand: true,
                cwd: 'src',
                src: ['assets/styles/*.scss'],
                dest: 'temp/',
                ext: '.css'
            }
        },
        // 转换js语法任务
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            main: {
                expand: true,
                cwd: 'src',
                src: ['assets/scripts/*.js'],
                dest: 'temp/'
            }
        },
        // 处理html文件任务，传递了data
        swig_render: {
            main: {
                options: {
                    data
                },
                expand: true,
                dest: 'temp/',
                cwd: 'src',
                src: ['**/*.html'],
                ext: '.html'  // 设置目标文件名以.html结尾
            },
        },
        // 图片字体文件压缩任务
        imagemin: {
            main: {
                files: [{
                    expand: true,
                    src: ['src/assets/images/*.{png,jpg,jpeg,svg}'],
                    dest: 'dist/'
                }]
            },
            fonts: {
                files: [{
                    expand: true,
                    src: ['src/assets/fonts/*.{png,jpg,jpeg,svg}'],
                    dest: 'dist/'
                }]
            },
        },
        // 监听对应的文件，然后调用对应文件的任务
        watch: {
            scripts: {
                files: ['src/assets/scripts/*.js'],
                tasks: ['babel'],
            },
            scss: {
                files: ['src/assets/styles/*.scss'],
                tasks: ['sass'],
            },
            pages: {
                files: ['src/**/*.html'],
                tasks: ['swig'],
            },
        },
        // 复制文件，将src 或 public 或 temp目录下的部分文件复制到dist目录
        copy: {
            fonts: {
                files: [{
                    expand: true,
                    src: ['src/assets/fonts/*'],
                    dest: 'dist/'
                }]
            },
            extra: {
                files: [{
                    expand: true,
                    cwd: "public",
                    src: ['*'],
                    dest: 'dist/'
                },]
            },
            build: {
                files: [{
                    expand: true,
                    cwd: "temp",
                    src: ['**/*'],
                    dest: 'dist'
                }],
            }
        },
        // 启动一个本地服务器，
        browserSync: {
            bsFiles: {
                src: 'temp/**/*'
            },
            options: {
                watchTask: true,
                notify: false,
                port: 8080,
                // 服务设置
                server: {
                    // 运行文件夹
                    baseDir: ['temp', 'src', 'public'],
                    // 配置映射，routes 高于 baseDir
                    routes: {
                        // 将 /node_modules 映射到工程下的node_modules
                        '/node_modules': 'node_modules'
                    },
                    keepalive: false
                }
            }
        },
    })
    grunt.registerTask('serve', ['browserSync', 'watch'])
    grunt.registerTask('compile', ['babel', 'sass', 'swig_render'])
    loadGruntTasks(grunt) // 使用loadGruntTasks 替代多个grunt.loadNpmTasks， 自动加在所有的插件任务
}